<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/3/31
 * Time: 9:19
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Member extends Base {
    /***
     * @return mixed|\think\response\Json
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('deploy')->count();// 获取总条数
            $lists = Db::name('deploy')
                ->page($NowPage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /***
     * @return mixed|\think\response\Json
     * 添加
     */
    public function add(){
        if(Request::instance()->isPost()){
            $gift_name =input('gift_name');
            $content= input('content');
            $gift_img= input('gift_img');
            $data['deploy_name'] = $gift_name;
            $data['deploy_content'] = $content;
            $data['deploy_img'] =$gift_img;
            $res = Db::table('up_deploy')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }

    /***
     * @return mixed|\think\response\Json
     * 编辑
     */
    public function update(){
        if(Request::instance()->isPost()){
            $id = input('id');
            $gift_name =input('gift_name');
            $content= input('content');
            $gift_img= input('gift_img');
            $data['deploy_name'] = $gift_name;
            $data['deploy_content'] = $content;
            if(!empty($gift_img)){
                $data['deploy_img'] =$gift_img;
            }
            $res = Db::table('up_deploy')->where('deploy_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $id = input('deploy_id');
        $info= Db::table('up_deploy')->where('deploy_id',$id)->find();
        $this->assign('id',$id);
        $this->assign('res',$info);
        return $this->fetch();
    }
}